<?php

namespace App\Http\Controllers;

use App\Model\Address;
use App\Model\Commodity;
use App\Model\Commodity_classify;
use App\Model\Commodity_color;
use App\Model\Commodity_info;
use App\Model\Commodity_size;
use App\Model\Order;
use App\Model\Parent_order;
use App\Model\User;
use App\Model\User_address;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TestController extends Controller
{
    private $request;
    private $parent_order;
    private $order;
    private $commodity_info;
    private $commodity_color;
    private $commodity_size;
    private $commodity_classify;
    private $commodity;
    private $address;
    private $user;
    private $user_address;

    function __construct(Request $request, Parent_order $parent_order, Order $order, Commodity_info $commodity_info, Commodity_color $commodity_color, Commodity_size $commodity_size, Commodity_classify $commodity_classify, Commodity $commodity, Address $address, User $user, User_address $user_address)
    {
        $this->request = $request;
        $this->parent_order = $parent_order;
        $this->order = $order;
        $this->commodity_info = $commodity_info;
        $this->commodity_color = $commodity_color;
        $this->commodity_size = $commodity_size;
        $this->commodity_classify = $commodity_classify;
        $this->commodity = $commodity;
        $this->address = $address;
        $this->user = $user;
        $this->user_address = $user_address;


    }

    public function index()
    {
        $ids = [38, 76, 64, 78];
        $commodityModel = new Commodity();
        $commodity = $commodityModel->get_commodity_by_commidity_ids($ids);
//        dd($commodity);
        if (!empty($commodity)) {
            $infoModel = new Commodity_info();
            foreach ($commodity as $key => $value) {
                $keys = $infoModel->get_info_keys_by_commodity((int)$value["commodity_id"]);
                preg_match('/(?<=commodity_info:).*?(?=(:commodity))/', $keys[0], $matches);
                if (empty($matches) || $matches[0] == '0') {
                    unset($commodity[$key]);
                    continue;
                }
                $res = $infoModel->mget_array_redis($keys);
                if (empty($res) || count($res) == 0)
                    unset($commodity[$key]);
                $commodity[$key]['commodity_info_price'] = reset($res)['commodity_info_price'];
            }
        }
        dd($commodity);
//        dd($data);
    }

    public function redis(Request $request)
    {
        $this->commodity->get_all_commodity();
        $this->commodity_size->get_all_size();
        $this->commodity_color->get_all_color();
        $this->commodity_info->get_all_info();
    }
}
